package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.modules.doctor.entity.DoctorAccountFlow;
import org.jeecg.common.modules.doctor.mapper.DoctorAccountFlowMapper;
import org.jeecg.common.modules.doctor.service.IDoctorAccountFlowService;
import org.jeecg.common.util.enums.AccountFlowTypeEnum;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: doctor_account_flow
 * @Author: jeecg-boot
 * @Date:   2020-08-22
 * @Version: V1.0
 */
@Service
public class DoctorAccountFlowServiceImpl extends ServiceImpl<DoctorAccountFlowMapper, DoctorAccountFlow> implements IDoctorAccountFlowService {

    @Override
    public List<DoctorAccountFlow> queryFlowByAccountId(String accountId) {
        if (StringUtils.isBlank(accountId)){
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<DoctorAccountFlow> query = Wrappers.lambdaQuery();
        query.eq(DoctorAccountFlow::getAccountId,accountId);
        return list(query);
    }

    @Override
    public DoctorAccountFlow addAccountFlow(String accountId, BigDecimal amount, AccountFlowTypeEnum flowType,String relationId){
        DoctorAccountFlow accountFlow = new DoctorAccountFlow();
        accountFlow.setAccountId(accountId);
        accountFlow.setAmount(amount);
        accountFlow.setFlowType(flowType.getCode());
        accountFlow.setCreateTime(new Date());
        accountFlow.setRelationId(relationId);
        save(accountFlow);
        return accountFlow;
    }

    @Override
    public DoctorAccountFlow getAccountFlow(String accountId, AccountFlowTypeEnum accountFlow, String relationId) {
        LambdaQueryWrapper<DoctorAccountFlow> query = Wrappers.lambdaQuery();
        query.eq(DoctorAccountFlow::getAccountId,accountFlow);
        query.eq(DoctorAccountFlow::getFlowType,accountFlow.getCode());
        query.eq(DoctorAccountFlow::getRelationId,relationId);
        return getOne(query);
    }

}
